<?php
/**
 * Author: Administrator
 * Date: 2022/7/23 22:56
 */

namespace DesignPatterns\Behavioral\Iterator;


class BookListIterator implements \Iterator
{
    protected BookList $bookList;
    protected int $currentIdx = 0;

    public function __construct(BookList $bookList)
    {
        $this->bookList = $bookList;
    }

    /**
     * @inheritDoc
     */
    public function current(): mixed
    {
        return $this->bookList->getBook($this->currentIdx);
    }

    /**
     * @inheritDoc
     */
    public function next(): void
    {
        $this->currentIdx++;
    }

    /**
     * @inheritDoc
     */
    public function key(): int
    {
        return $this->currentIdx;
    }

    /**
     * @inheritDoc
     */
    public function valid(): bool
    {
        return $this->currentIdx < count($this->bookList);
    }

    /**
     * @inheritDoc
     */
    public function rewind(): void
    {
        $this->currentIdx = 0;
    }
}